package org.sardine.aop;

import java.util.ArrayList;
import java.util.List;

import org.aopalliance.aop.Advice;
import org.apache.log4j.Logger;
import org.sardine.beans.BeanPostProcessor;

/**
 * 
 * 为每个实例都创建代理，只在实例方法被调用时才判定是否使用拦截器
 * 
 * @author BearBear
 *
 */
public class MethodsPointcutAdvisor implements BeanPostProcessor{
	
	private final Logger log = Logger.getLogger(MethodsPointcutAdvisor.class);
	
	private List<Advice> interceptors = new ArrayList<Advice>();
	private List<String> classNames = new ArrayList<String>();
	private List<String> methodNamePatterns = new ArrayList<String>();
	private boolean cglib = false;
	
	public Object postProcessAfterInitialization(String beanName, Object bean) {
		
		
		String beanClass = bean.getClass().getName();
		Object beanProxy = null;
		
		log.info("create "+beanClass+" proxy");
		
		AopProxyCreator proxyCreator = new AopProxyCreator(bean,cglib);
		proxyCreator.setInterceptors(interceptors);
		proxyCreator.setMethodNamePatterns(methodNamePatterns);
		proxyCreator.setUseFullyQualifiedName(true);

		beanProxy = proxyCreator.getProxy();
		
		
		return beanProxy;
	}

	public List<Advice> getInterceptors() {
		return interceptors;
	}

	public List<String> getClassNames() {
		return classNames;
	}

	public boolean isCglib() {
		return cglib;
	}

	public void setInterceptors(List<Advice> interceptors) {
		this.interceptors = interceptors;
	}

	public void setClassNames(List<String> classNames) {
		this.classNames = classNames;
	}

	public void setCglib(boolean cglib) {
		this.cglib = cglib;
	}

	public List<String> getMethodNamePatterns() {
		return methodNamePatterns;
	}

	public void setMethodNamePatterns(List<String> methodNamePatterns) {
		this.methodNamePatterns = methodNamePatterns;
	}
	
	

}
